package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Employee;
import com.sky.mapper.ClassificationMapper;
import com.sky.result.PageResult;
import com.sky.service.ClassificationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClassificationServiceImpl implements ClassificationService {

    @Autowired
    private ClassificationMapper classificationMapper;

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult getPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        Page<Category> page = classificationMapper.pageQuery(categoryPageQueryDTO);

        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());

        return pageResult;
    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);

        category.setStatus(StatusConstant.ENABLE);
        category.setCreateTime(LocalDateTime.now() );
        category.setUpdateTime(LocalDateTime.now() );
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        classificationMapper.Category_add(category);
    }

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void deleteCategory(Integer id) {
        classificationMapper.deleteById(id);
    }

    /**
     * 启用禁用分类
     * @param status
     * @param id
     */
    @Override
    public void statusSOS(Integer status,Long id) {
        Category category = Category.builder()
                .status(status)
                .id(id)
                .build();
        classificationMapper.update(category);
    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    public void updateC(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        category.setUpdateTime(LocalDateTime.now() );
        category.setUpdateUser(BaseContext.getCurrentId());
        classificationMapper.update(category);
    }


    /**
     * 根据搜索类型或名字查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> getListByType(Integer type) {
        List<Category> list = classificationMapper.getListByType(type);
        return list;
    }
}
